﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.LanguageWire.DAL.iLanglDAL;
using System.Data;
using iLangl.LanguageWire.DAL.LanguageWireDAL;

namespace iLangl.LanguageWire.Managers
{
	public class ProjectManager : BaseManager
	{
		#region Singelton Pattern
		private ProjectManager()
		{
		}

		private static ProjectManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the ProjectManager.
		/// </summary>
		public static ProjectManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new ProjectManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion


		

		public void RegisterProject(ProjectLW project)
		{
			IDbTransaction transaction = null;

			//Store All Data in iLangl before proceed
			if (project.Instance.Owner != null)
			{
				if (project.Instance.Owner.Company != null)
					DALFacade.Instance.CompanyDAL.CreateCompany((Company)project.Instance.Owner.Company, transaction);

				DALFacade.Instance.ContactDAL.CreateContact(project.Instance.Owner, transaction);
			}

			if (project.Instance.TechnicalContact != null)
			{
				if (project.Instance.TechnicalContact.Company != null)
					DALFacade.Instance.CompanyDAL.CreateCompany((Company)project.Instance.TechnicalContact.Company, transaction);

				DALFacade.Instance.ContactDAL.CreateContact(project.Instance.TechnicalContact, transaction);
			}



			DALFacade.Instance.ProjectDAL.RegisterProject(project.Instance, transaction);

			Company company = (Company)project.GetProjectCompany();

			CompanyManager.Instance.CreateCompany(company);

			if(company.BillingInfo != null)
				InvoiceManager.Instance.CreateInvoice(company.BillingInfo);

			ContactManager.Instance.CreatePrimaryContact(project.Instance.Owner);

			if (company.Contacts != null)
				foreach (Contact contact in company.Contacts)
				{
					ContactManager.Instance.CreateContact(contact);
				}

			DALFacadeLW.Instance.ProjectDAL.CreateProject(project, transaction);

			foreach (Translation translation in project.Instance.Translations)
			{
				if (String.IsNullOrEmpty(translation.TPIdentity))
				{
					continue;
				}

				int jobId = int.Parse(translation.TPIdentity);

				translation.Responsible =  ContactManager.Instance.GetResponsiblePerson(jobId);
				translation.Responsible.Company = CompanyManager.Instance.GetLWCompanyInfo();
				DALFacadeiLangl.TranslationDAL.UpdateTranslation(translation, transaction);
			}

			SynchronizationManager.Instance.RegisterProjectToSync(project);
		}


		public Project GetProject(String projectId)
		{
			return DALFacade.Instance.ProjectDAL.ReadProject(projectId);
		}

		/// <summary>
		/// If project in progress and all translations finished we change project status to finished
		/// </summary>
		/// <param name="projectId"></param>
		public void UpdateProjectStatus(string projectId)
		{
			Project project = GetProject(projectId);
			IList<Translation> translations = DALFacade.Instance.TranslationDAL.ReadTranslationsByProject(project);

			ProjectStatuses.eStatus status = ProjectStatuses.GetStatus(project.Status);

			if (status == ProjectStatuses.eStatus.InProgress)
			{
				bool isAllTranslationFinished = true;
				foreach (Translation translation in translations)
				{
					TranslationStatuses.eTranslationStatus translationstatus = TranslationStatuses.GetStatus(translation.Status);

					if (translationstatus != TranslationStatuses.eTranslationStatus.Finished)
					{
						isAllTranslationFinished = false;
						break;
					}
				}

				if (isAllTranslationFinished)
				{
					project.Status = ProjectStatuses.GetStatus(ProjectStatuses.eStatus.Finished);
					DALFacade.Instance.ProjectDAL.UpdateProjectStatus(project, null);

					SynchronizationManager.Instance.RemoveProjectFromSync(new ProjectLW(project));
				}
			}

		}

		public void UpdateProject(ProjectLW project)
		{
			IDbTransaction transaction = null;

			//Store All Data in iLangl before proceed
			if (project.Instance.Owner != null)
			{
				if (project.Instance.Owner.Company != null)
					DALFacade.Instance.CompanyDAL.UpdateCompany((Company)project.Instance.Owner.Company, transaction);

				DALFacade.Instance.ContactDAL.UpdateContact(project.Instance.Owner, transaction);
			}

			if (project.Instance.TechnicalContact != null)
			{
				if (project.Instance.TechnicalContact.Company != null)
					DALFacade.Instance.CompanyDAL.UpdateCompany((Company)project.Instance.TechnicalContact.Company, transaction);

				DALFacade.Instance.ContactDAL.UpdateContact(project.Instance.TechnicalContact, transaction);
			}



			DALFacade.Instance.ProjectDAL.UpdateProject(project.Instance, transaction);

			Company company = (Company)project.GetProjectCompany();

			CompanyManager.Instance.UpdateCompany(company);
		
			foreach (Translation translation in project.Instance.Translations)
			{
				DALFacadeiLangl.TranslationDAL.UpdateTranslation(translation, transaction);
			}

			SynchronizationManager.Instance.RegisterProjectToSync(project);
		}
	}

}
